<template>
	<view>
		<u-mask :show="showMask" @click="show = false">
			<view style="height: 100%;">
				<view class="rect" @tap.stop style="height: 100%;">
					<view style="background-color: white;height: 100%;">
						<view style="padding: 20rpx 30rpx;">
							<u-search inputAlign="center" placeholder="请输入关键词" v-model="keyword" :show-action="false"
								@search="list=[];querySearchAsync(keyword)" @click="data=[];"></u-search>
						</view>
						<view>
							<u-cell-group>
								<scroll-view :scroll-y="true" style="height: calc(100vh - 150rpx);">
									<u-cell-item :title="item.title"  v-for="item in list" @click="select(item)">
										<view slot="label">
											<view>{{item.address}}</view>
										</view>
									</u-cell-item>
								</scroll-view>
							</u-cell-group>
						</view>
					</view>
				</view>
			</view>
		</u-mask>
	</view>
</template>

<script>
	export default {
		name: "selectAddress",
		data() {
			return {
				keyword: '',
				showMask: false,
				list: [],
				type:''
			};
		},
		mounted() {
			this.showMask = false
		},
		methods: {
			select(item){
				item.type=this.type;
				this.$emit('select',item);
				this.showMask=false;
				this.type='';
			},
			show(type) {
				this.type=type;
				this.showMask = true
			},
			extractTextInParentheses(str) {
				// 使用正则表达式匹配圆括号内的内容
				// g 标志表示全局匹配，匹配所有圆括号内的内容
				const regex = /\((.*?)\)/g;
				const matches = [];
				let match;

				// 使用循环获取所有匹配结果
				while ((match = regex.exec(str)) !== null) {
					// match[1] 包含圆括号内的内容，不包括圆括号本身
					matches.push(match[1]);
				}

				return matches;
			},
			querySearchAsync(text, cb) {
				var that = this;
				if (text.trim() == '') {
					return;
				}
				var that = this;
				uni.request({
					url: 'https://apis.map.qq.com/ws/place/v1/suggestion',
					data: {
						key: 'C77BZ-UKERR-D6EWG-WMRUN-JG4J2-ZBF5Y',
						region: "喀什",
						keyword: text,
						output: "jsonp",
					},
					success(res) {
						var list = res.data;
						list=list.substring(list.indexOf('(')+1,list.lastIndexOf(')'));
						list=JSON.parse(list).data;
						that.list=list;
						// var list=JSON.parse(that.extractTextInParentheses(that.list)).data;
						// console.log(that.extractTextInParentheses(res.data));
					}
				})
			},
		}
	}
</script>

<style>

</style>